home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / Communications Toolbox / CTB Sample Code 1.0b16 / CTB Sources / Sources 2 / Connection Tool for CTB / cdef.c next >
Encoding:
C/C++ Source or Header  |  1989-10-06  |  12.8 KB  |  492 lines  |  [TEXT/MPS ]

  1. /************************************************************************************
  2. *
  3. *  Project Name:    Templates
  4. *     File Name:    cdef.c
  5. *        Author:    Rob Neville (IIx)
  6. *          Date:    May 17, 1989
  7. *
  8. *   Description:    Template cdef function. 
  9. *
  10. *************************************************************************************
  11. *
  12. *    Revision History:
  13. *        5/17/89 - Original version by Rob Neville (IIx)
  14. *        6/26/89 - Reved for b2 of Comm Toolbox
  15. *
  16. ************************************************************************************/
  17.  
  18. #include "CMTool.h"
  19.  
  20. pascal long CDEF(hTH,msg,p1,p2,p3)
  21. ToolHandle    hTH;
  22. short        msg;
  23. long        p1,p2,p3;
  24. {
  25.     long    theErr;
  26.     
  27.     long     ToolInit(ToolHandle);
  28.     long    ToolDispose(ToolHandle);
  29.     long    ToolSuspend(ToolHandle);
  30.     long    ToolResume(ToolHandle);
  31.     long    ToolMenu(ToolHandle,short,short);
  32.     long    ToolEvent(ToolHandle,EventRecord *);
  33.     long     ToolActivate(ToolHandle);
  34.     long    ToolDeactivate(ToolHandle);
  35.     long     ToolReset(ToolHandle);
  36.     long    ToolIdle(ToolHandle);
  37.     long    ToolAbort(ToolHandle);
  38.     long    ToolRead(ToolHandle,CMDataBufferPtr,long,CMCompletorPtr);
  39.     long    ToolWrite(ToolHandle,CMDataBufferPtr,long,CMCompletorPtr);
  40.     long    ToolStatus(ToolHandle,CMBufferSizes,CMStatFlags *);
  41.     long    ToolListen(ToolHandle,CMCompletorPtr,long);
  42.     long    ToolAccept(ToolHandle,Boolean);
  43.     long    ToolClose(ToolHandle,CMCompletorPtr,Boolean);
  44.     long     ToolOpen(ToolHandle,CMCompletorPtr,long);
  45.     long    ToolBreak(ToolHandle,long,CMCompletorPtr);
  46.     long    ToolIOKill(ToolHandle,short);
  47.     long    ToolEnvirons(ToolHandle,ConnEnvironRecPtr);
  48.     
  49.     switch (msg) {
  50.         case TInitMsg:
  51.             theErr = ToolInit(hTH);                        
  52.             break;
  53.         case TDisposeMsg:
  54.             theErr = ToolDispose(hTH);                    
  55.             break;
  56.         case TSuspendMsg:
  57.             theErr = ToolSuspend(hTH);                    
  58.             break;
  59.         case TResumeMsg:
  60.             theErr = ToolResume(hTH);                                
  61.             break;
  62.         case TMenuMsg:
  63.             theErr = ToolMenu(hTH,(short)p1,(short)p2);    
  64.             break;
  65.         case TEventMsg:
  66.             theErr = ToolEvent(hTH,(EventRecord *)p1);    
  67.             break;
  68.         case TActivateMsg:
  69.             theErr = ToolActivate(hTH);                    
  70.             break;
  71.         case TDeactivateMsg:
  72.             theErr = ToolDeactivate(hTH);                    
  73.             break;
  74.         case TResetMsg:
  75.             theErr = ToolReset(hTH);
  76.             break;
  77.         case TIdleMsg:
  78.             theErr = ToolIdle(hTH);
  79.             break;
  80.         case TAbortMsg:
  81.             theErr = ToolAbort(hTH);    
  82.             break;
  83.         case TReadMsg:
  84.             theErr = ToolRead(hTH,(CMDataBufferPtr)p1,p2,(CMCompletorPtr)p3);
  85.             break;
  86.         case TWriteMsg:
  87.             theErr = ToolWrite(hTH,(CMDataBufferPtr)p1,p2,(CMCompletorPtr)p3);
  88.             break;
  89.         case TStatusMsg:
  90.             theErr = ToolStatus(hTH,(CMBufferSizes)p1,(CMStatFlags *)p2);
  91.             break;
  92.         case TListenMsg:
  93.             theErr = ToolListen(hTH,(CMCompletorPtr)p1,p2);
  94.             break;
  95.         case TAcceptMsg:
  96.             theErr = ToolAccept(hTH,(Boolean)p1);
  97.             break;
  98.         case TCloseMsg:
  99.             theErr = ToolClose(hTH,(CMCompletorPtr)p1,(Boolean)p2);
  100.             break;
  101.         case TOpenMsg:
  102.             theErr = ToolOpen(hTH,(CMCompletorPtr)p1,p2);
  103.             break;
  104.         case TBreakMsg:
  105.             theErr = ToolBreak(hTH,p1,(CMCompletorPtr)p2);
  106.             break;
  107.         case TIOKillMsg:
  108.             theErr = ToolIOKill(hTH,(short)p1);
  109.             break;
  110.         case TEnvironsMsg:
  111.             theErr = ToolEnvirons(hTH,(ConnEnvironRecPtr)p1);
  112.             break;
  113.         default:                        /* we should never get this */
  114.             theErr = TNotSupported;        /* I don't support unsupported messages */
  115.             break;
  116.     }
  117.     return (theErr);
  118. }
  119.  
  120. long ToolInit(ToolHandle hTH)
  121. {
  122.     long         result = TNoErr;
  123.     PrivatePtr    cmPrivate;
  124.     char        saved;
  125.     
  126.     if ((**hTH).procID < 3)    {        /* procID's less than 3 are reserved for the System or are errors */
  127.         debugstr("Pipe is blocked!!! Call New!!;hc;g");
  128.         result = TFailed;
  129.     }
  130.     else        /* This portion will change for other Tool Types */
  131.     {
  132.         
  133.         /* Debugger(); */
  134.         saved = HGetState((Handle)hTH);
  135.         HLock((Handle)hTH);
  136.         (**hTH).bufferArray[cmDataIn] = NewPtrClear(1024);
  137.         if ((**hTH).bufferArray[cmDataIn] == nil)
  138.         {
  139.             (**hTH).errCode = MemError();
  140.             return ((**hTH).errCode);
  141.         }
  142.         (**hTH).bufferArray[cmDataOut] = NewPtrClear(1024);
  143.         if ((**hTH).bufferArray[cmDataOut] == nil)
  144.         {
  145.             (**hTH).errCode = MemError();
  146.             DisposPtr((**hTH).bufferArray[cmDataIn]);
  147.             return ((**hTH).errCode);
  148.         }
  149.         HSetState((Handle)hTH,saved);
  150.         cmPrivate = (PrivatePtr)NewPtrClear(sizeof(cmPrivate));
  151.     
  152.         if (cmPrivate == nil)
  153.         {
  154.             (**hTH).errCode = MemError();
  155.             DisposPtr((**hTH).bufferArray[cmDataIn]);
  156.             DisposPtr((**hTH).bufferArray[cmDataOut]);
  157.             result = ((**hTH).errCode);
  158.         }
  159.         (PrivatePtr)(**hTH).cmPrivate = cmPrivate;
  160.         (**hTH).flags |= cmData;
  161.         (**hTH).errCode = TNoErr;
  162.     }
  163.     return (result);
  164. }
  165.  
  166. long ToolDispose(ToolHandle hTH)
  167. {
  168.     long result = TNoErr;
  169.     
  170.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  171.         debugstr("Pipe is blocked!!! Call Dispose!!;hc;g");
  172.         result = TFailed;
  173.     }
  174.     else
  175.         DisposPtr((**hTH).cmPrivate);            /* dispose my only data structure */
  176.     return (result);
  177. }
  178.  
  179. long ToolSuspend(ToolHandle hTH)
  180. {
  181.     long     result = TNoErr;
  182.     
  183.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  184.         debugstr("Pipe is blocked!!! Call Suspend!!;hc;g");
  185.         result = TFailed;
  186.     }
  187.     return (result);
  188. }
  189.  
  190. long ToolResume(ToolHandle hTH)
  191. {
  192.     long     result = TNoErr;
  193.     
  194.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  195.         debugstr("Pipe is blocked!!! Call Resume!!;hc;g");
  196.         result = TFailed;
  197.     }
  198.     return (result);
  199. }
  200.  
  201. long ToolMenu(ToolHandle hTH,short p1,short p2)
  202. {
  203.     long    result = TNoErr;
  204.     
  205.     if (((**hTH).procID < 3 ) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  206.         debugstr("Pipe is blocked!!! Call Menu!! In Handle;hc;g");
  207.         (**hTH).errCode = TFailed;
  208.     }
  209.     if (p1 >= 32767 || p1 <= -32767) {            /* Need to check if it is a valid short */ 
  210.         debugstr("Pipe is blocked!!! Call Menu!! Invalid p1;hc;g");
  211.         (**hTH).errCode = TFailed;
  212.     }
  213.     if (p2 >= 32767 || p2 <= -32768) {            /* Need to check if it it a valid short */
  214.         debugstr("Pipe is blocked!!! Call Menu!! Invalid p2;hc;g");
  215.         (**hTH).errCode = TFailed;
  216.     }
  217.     return (result);
  218. }
  219.  
  220. long ToolEvent(ToolHandle hTH,EventRecord *theEvent) /* Don't pass this a NullEvent PLEASE */
  221. ToolHandle     hTH;
  222. {
  223.     long     result = TNoErr;
  224.     
  225.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  226.         debugstr("Pipe is blocked!!! Call Event!! In Handle;hc;g");
  227.         result = TFailed;
  228.     }
  229.     if (theEvent->what <= 0 ) {        /* Invalid EventRecord if what < 0 */
  230.         debugstr("Pipe is blocked!!! Call Event!! p1;hc;g");
  231.         result = TFailed;
  232.     }
  233.     return (result);
  234. }
  235.  
  236. long ToolActivate(ToolHandle hTH)
  237. {
  238.     long     result = TNoErr;
  239.     
  240.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  241.         debugstr("Pipe is blocked!!! Call Activate!!;hc;g");
  242.         result = TFailed;
  243.     }
  244.     return (result);
  245. }
  246.  
  247. long ToolDeactivate(ToolHandle hTH)
  248. {
  249.     long     result = TNoErr;
  250.     
  251.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  252.         debugstr("Pipe is blocked!!! Call Deactivate!!;hc;g");
  253.         result = TFailed;
  254.     }
  255.     return (result);
  256. }
  257.  
  258. long ToolReset(ToolHandle hTH)
  259. {
  260.     long     result = TNoErr;
  261.     
  262.     if (((**hTH).procID < 3 ) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  263.         debugstr("Pipe is blocked!!! Call Abort!!;hc;g");
  264.         result = TFailed;
  265.     }
  266.     return (result);
  267. }
  268.  
  269. long ToolIdle(ToolHandle hTH)
  270. {
  271.     long     result = TNoErr;
  272.     
  273.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  274.         debugstr("Pipe is blocked!!! Call Abort!!;hc;g");
  275.         result = TFailed;
  276.     }
  277.     return (result);
  278. }
  279.  
  280. long ToolAbort(ToolHandle hTH)
  281. {
  282.     long     result = TNoErr;
  283.     
  284.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  285.         debugstr("Pipe is blocked!!! Call Abort!!;hc;g");
  286.         result = TFailed;
  287.     }
  288.     return (result);
  289. }
  290.  
  291. long ToolRead(ToolHandle hTH,CMDataBufferPtr p1,long p2,CMCompletorPtr p3)
  292. {
  293.     long     result = TNoErr;
  294.     
  295.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  296.         debugstr("Pipe is blocked!!! Call Start!! In Handle;hc;g");
  297.         result = TFailed;
  298.     }
  299.     if (p1 <  0) { /* Can't be a valid ptr if < 0 */
  300.         debugstr("Pipe is blocked!!! Call Start!! In p1;hc;g");
  301.         result = TFailed;
  302.     }
  303.     if (p2 <  0) { 
  304.         debugstr("Pipe is blocked!!! Call Start!! In p2;hc;g");
  305.         result = TFailed;
  306.     }
  307.     if (p3 <  0) { /* Con't be a valid ptr if less than zero */
  308.         debugstr("Pipe is blocked!!! Call Start!! In p3;hc;g");
  309.         result = TFailed;
  310.     }
  311.     return (result);
  312. }
  313.  
  314. long ToolWrite(ToolHandle hTH,CMDataBufferPtr p1,long p2,CMCompletorPtr p3)
  315. {
  316.     long     result = TNoErr;
  317.     
  318.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  319.         debugstr("Pipe is blocked!!! Call Start!! In Handle;hc;g");
  320.         result = TFailed;
  321.     }
  322.     if (p1 <  0) {  /* Can't be a valid ptr */
  323.         debugstr("Pipe is blocked!!! Call Start!! In p1;hc;g");
  324.         result = TFailed;
  325.     }
  326.     if (p2 <  0) {
  327.         debugstr("Pipe is blocked!!! Call Start!! In p2;hc;g");
  328.         result = TFailed;
  329.     }
  330.     if (p3 <  0) {  /* Can't be a valid ptr */
  331.         debugstr("Pipe is blocked!!! Call Start!! In p3;hc;g");
  332.         result = TFailed;
  333.     }
  334.     return (result);
  335. }
  336.  
  337. long ToolStatus(ToolHandle hTH,CMBufferSizes p1,CMStatFlags *p2)
  338. {
  339.     long     result = TNoErr;
  340.     
  341.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  342.         debugstr("Pipe is blocked!!! Call Status!! In Handle;hc;g");
  343.         result = TFailed;
  344.     }
  345.     if (p1 <  0) {
  346.         debugstr("Pipe is blocked!!! Call Status!! In p1;hc;g");
  347.         result = TFailed;
  348.     }
  349.     else {
  350.         p1[cmDataIn] = 0;
  351.         p1[cmDataOut] = 0;
  352.     }
  353.     if (p2 <  0) {
  354.         debugstr("Pipe is blocked!!! Call Status!! In p2;hc;g");
  355.         result = TFailed;
  356.     }
  357.     else
  358.         *p2 = 0;
  359.     return (result);
  360. }
  361.  
  362. long ToolAccept(ToolHandle hTH,Boolean p1)
  363. {
  364.     long     result = TNoErr;
  365.     
  366.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  367.         debugstr("Pipe is blocked!!! Call Accept!!;hc;g");
  368.         result = TFailed;
  369.     }
  370.     if (p1 <  0) {
  371.         debugstr("Pipe is blocked!!! Call Accept!! In p1;hc;g");
  372.         result = TFailed;
  373.     }
  374.     return (result);
  375. }
  376.  
  377. long ToolClose(ToolHandle hTH,CMCompletorPtr p1,Boolean p2)
  378. {
  379.     long     result = TNoErr;
  380.     
  381.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  382.         debugstr("Pipe is blocked!!! Call Close!!;hc;g");
  383.         result = TFailed;
  384.     }
  385.     if (p1 <  0) {
  386.         debugstr("Pipe is blocked!!! Call Close!! In p1;hc;g");
  387.         result = TFailed;
  388.     }
  389.     if (p2 <  0) {
  390.         debugstr("Pipe is blocked!!! Call Close!! In p2;hc;g");
  391.         result = TFailed;
  392.     }
  393.     return (result);
  394. }
  395.  
  396. long ToolOpen(ToolHandle hTH,CMCompletorPtr p1,long p2)
  397. {
  398.     long     result = TNoErr;
  399.     
  400.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  401.         debugstr("Pipe is blocked!!! Call Open!!;hc;g");
  402.         result = TFailed;
  403.     }
  404.     if (p1 <  0) {
  405.         debugstr("Pipe is blocked!!! Call Open!! In p1;hc;g");
  406.         result = TFailed;
  407.     }
  408.     if (p2 <  -1) {
  409.         debugstr("Pipe is blocked!!! Call Open!! In p2;hc;g");
  410.         result = TFailed;
  411.     }
  412.     return (result);
  413. }
  414.  
  415. long ToolListen(ToolHandle hTH,CMCompletorPtr p1,long p2)
  416. {
  417.     long     result = TNoErr;
  418.     
  419.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  420.         debugstr("Pipe is blocked!!! Call Listen!!;hc;g");
  421.         result = TFailed;
  422.     }
  423.     if (p1 <  0) {
  424.         debugstr("Pipe is blocked!!! Call Listen!! In p1;hc;g");
  425.         result = TFailed;
  426.     }
  427.     if (p2 <  0) {
  428.         debugstr("Pipe is blocked!!! Call Listen!! In p2;hc;g");
  429.         result = TFailed;
  430.     }
  431.     return (result);
  432. }
  433.  
  434.  
  435. long ToolBreak(ToolHandle hTH,long p1,CMCompletorPtr p2)
  436. {
  437.     long     result = TNoErr;
  438.     
  439.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  440.         debugstr("Pipe is blocked!!! Call Break!!;hc;g");
  441.         result = TFailed;
  442.     }
  443.     if (p1 <  0) {
  444.         debugstr("Pipe is blocked!!! Call Break!! In p1;hc;g");
  445.         result = TFailed;
  446.     }
  447.     if (p2 <  0) {
  448.         debugstr("Pipe is blocked!!! Call Break!! In p2;hc;g");
  449.         result = TFailed;
  450.     }
  451.     return (result);
  452. }
  453.  
  454. long ToolIOKill(ToolHandle hTH,short p1)
  455. {
  456.     long     result = TNoErr;
  457.     
  458.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  459.         debugstr("Pipe is blocked!!! Call IOKill!!;hc;g");
  460.         result = TFailed;
  461.     }
  462.     if (p1 <  0) {
  463.         debugstr("Pipe is blocked!!! Call IOKill!! In p1;hc;g");
  464.         result = TFailed;
  465.     }
  466.     return (result);
  467. }
  468.  
  469. long ToolEnvirons(ToolHandle hTH,ConnEnvironRecPtr p1)
  470. {
  471.     long     result = TNoErr;
  472.     
  473.     if (((**hTH).procID < 3) || ((**hTH).cmPrivate == nil)) { /* Invalid procID or no cmPrivatePtr */
  474.         debugstr("Pipe is blocked!!! Call Environs!!;hc;g");
  475.         result = TFailed;
  476.     }
  477.     if (p1 <  0) {
  478.         debugstr("Pipe is blocked!!! Call Environs!! In p1;hc;g");
  479.         result = TFailed;
  480.     }                            /* Should set something in case other Tools want to know */
  481.     p1->baudRate = 0;            /* no baud rate this is a dummy tool */
  482.     p1->dataBits = 8;            /* 8 bit path */
  483.     p1->swFlowControl = 0;        /* no software handshaking this is a dummy tool */
  484.     p1->hwFlowControl = 0;        /* no hardware handshaking this is a dummy tool */
  485.     p1->channels = cmData;        /* Data channel only */
  486.     if (p1->version < 0)
  487.         result = envBadVers;    /* version less than zero is BAD */
  488.     if (p1->version > 0)
  489.         result = envVersTooBig;    /* version larger than zero is too big at this time */
  490.     return (result);
  491. }
  492.